<?php
/**
 * Menu Class
 *
 * Navigation List with current page check
 *
 * @package		CodeIgniter
 * @category	Libraries
 * @start		4.43 02/11/2008
 * @version		1.3  - 15.30 17/10/2008
 * @credit		Fork: Html_Helper (CodeIgniter)
 * @author		Piro Fabio (dewosmail@gmail.com)
 */
 
class Menu 
{
	/**
	 * Constructor
	 */
	 
	function Menu()
	{
		$this->ci =& get_instance();
		$this->ci->config->load('menu');
		
		log_message('debug', 'Menu Library: Initialized');
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Get Array and inizialize the creation
	 *
	 * @access public
	 * @param string 	direct array OR in application/config/menu.php 
	 * @param string 	type of list ( OL | UL )
	 * @return 
	 */
	 
	function create($list, $type = 'ul', $attributes='')
	{
		// No direct Array submitted?
		if ( ! is_array($list))
		{
			//Load the Array from Config File
			$list = $this->ci->config->item($list);
		}
		
		return $this->_list($list, $type, $attributes);
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Generate Menu List with current Styling and all...
	 *
	 * @access public
	 * @param array 	the menu array
	 * @param string 	type of list ( OL | UL )
	 * @return string
	 */
	 
	function _list($list, $type = 'ul', $attributes = '', $depth = 0)
	{
		// Nothing to do...
		if ( ! is_array($list))
		{
			return $list;
		}
		
		// Indent
		$out = str_repeat(" ", $depth);
		
		// Attributes submitted? 
		if (is_array($attributes))
		{
			$atts = '';
			foreach ($attributes as $key => $val)
			{
				$atts .= ' ' . $key . '="' . $val . '"';
			}
			$attributes = $atts;
		}
		
		// Opening
		$out .= "<" . $type  . $attributes . ">\n";
		
		// Cycle through the list elements.  
		// If an array is encountered we will recursively call _create()
		static $_last_list_item = '';
		foreach ( $list as $key => $val )
		{
			// Var
			$status = '';
			$_last_list_item = $key;
			
			// Indent
			$out .= str_repeat(" ", $depth + 2);
			
			// Check  if it's the current page per styling
			if ( ! is_array($val))
			{
				$status = $this->_check_current($val);
			}
			
			// Open Li
			$out .= "<li" . $status . ">";
		
			// Any Children Array??
			if ( ! is_array($val))
			{
				$out .= $val;
			}
			// More Depth Found... recursively call
			else
			{
				$out .= $_last_list_item."\n";
				$out .= $this->_list($val, $type, '', $depth + 4);
				$out .= str_repeat(" ", $depth + 2);
			}
			
			// Close Li
			$out .= "</li>\n";		
		}
		
		// Indent
		$out .= str_repeat(" ", $depth);

		// Closing
		$out .= "</" . $type . ">\n";
		
		return $out;
	}

	// ------------------------------------------------------------------------
	
	/**
	 * Check Current Page
	 *
	 * @access	public
	 * @parm	string	the link uri for matchets
	 * @return	string
	 */	

	function _check_current($match)
	{
		// Var
		$current_tag = $this->ci->config->item('current_tag');
		$segment = get_class($this->ci);// Current Controller Name
		//$segment = uri_string();
		//echo $segment . '-' . $match ."<br>";
		// Check the existence of a case-insensitive string
		// in the link, matched with current url
		return (strripos($match,  $segment) == TRUE) ? ' class=" ' . $current_tag .'"' : NULL;
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Check Right Position Entry
	 *
	 * @access	public
	 * @parm	string	the link uri for matchets
	 * @return	string
	 */	

	function _check_right_position($match)
	{
		// Var
		$right_tag = $this->ci->config->item('right_tag');
		$right_entries = $this->ci->config->item('right_entries');

		echo $match . '-';
		print_r($right_entries);
		echo '<br />';

		// Check the existence of a case-insensitive string
		// in the link, matched with current url
		return (in_array($match, $right_entries) == TRUE) ? ' class=" ' . $right_tag .'"' : NULL;
	}
	
	// ------------------------------------------------------------------------
}
/* End of file menu.php */
/* Location: ./application/libraries/menu.php */